home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / UTILITIE / DOS_TEX.ZIP / TEX2ASC.C < prev    next >
C/C++ Source or Header  |  1993-07-15  |  7KB  |  295 lines

  1. #include <stdio.h>
  2. #ifdef __STDC__
  3. # include <stdlib.h>
  4. #endif
  5. #include <ctype.h>
  6. #include <string.h>
  7.  
  8. #define ML 512
  9. #define streq(A,B) (strcmp((A),(B))==0)
  10. #ifndef TRUE
  11. # define TRUE 1
  12. # define FALSE 0
  13. #endif
  14.  
  15. int indent;
  16. int wrap_index = 78;
  17.  
  18. #ifdef __STDC__
  19. void kilspaces(char *t)
  20. #else
  21. kilspaces(t)
  22. char *t;
  23. #endif
  24. {
  25.      int i=0, j=0;
  26.  
  27.      strcat(t,"   ");
  28.  
  29.      while(t[i]!='\0')
  30.      {
  31.           while( t[i]!='\0' && strchr("\t\r\n ",t[i]) )
  32.                i++;
  33.           while( t[i]!='\0' && !strchr("\t\r\n ",t[i]) )
  34.           {
  35.                t[j]=t[i];
  36.                j++;
  37.                i++;
  38.           }
  39.           t[j++]=' ';
  40.      }
  41.      t[j]='\0';
  42.  
  43.      return;
  44. }
  45.  
  46. #ifdef __STDC__
  47. void force_out(char *t)
  48. #else
  49. force_out(t)
  50. char *t;
  51. #endif
  52. {
  53.      int i, j;
  54.  
  55.      kilspaces(t);
  56.  
  57.      if(t[0]=='\0')
  58.      {
  59.           putchar('\n');
  60.           return;
  61.      }
  62.  
  63.      if(strlen(t)>wrap_index)
  64.      {
  65.           for(i=wrap_index-5*indent;t[i]!=' ';i--)
  66.                ;
  67.           t[i++]='\0';   /* i indexes the beginning of the next string */
  68.      }
  69.      else
  70.           i=0;
  71.  
  72.      if(indent)
  73.           fputs("     ",stdout);
  74.  
  75.      for(j=0;t[j]!='\0';j++)
  76.      {
  77.           if(t[j]=='~')
  78.                putchar(' ');
  79.           else if(t[j]=='.' && t[j+1]==' ')
  80.                fputs(". ",stdout);
  81.           else
  82.                putchar(t[j]);
  83.      }
  84.      putchar('\n');
  85.  
  86.      if(i)
  87.           strcpy(t,t+i);
  88.      else
  89.           t[0]='\0';
  90.  
  91.      indent=FALSE;
  92.  
  93.      return;
  94. }
  95.  
  96. #ifdef __STDC__
  97. void usage(void)
  98. #else
  99. usage()
  100. #endif
  101. {
  102.      puts("usage: tex2asc [-w wrap_limit]");
  103.      return;
  104. }
  105.  
  106. #ifdef __STDC__
  107. int main(int argc, char **argv)
  108. #else
  109. main(argc, argv)
  110. int argc;
  111. char **argv;
  112. #endif
  113. {
  114.      char c, cc, s[ML], t[ML];
  115.      int i, cr=0, t_index=0, chapter=1;
  116.      int control=FALSE, print_line=FALSE, center=FALSE;
  117.  
  118.      while(*++argv!=NULL)
  119.      {
  120.           if((*argv)[0]!='-')
  121.           {
  122.                usage();
  123.                return 1;
  124.           }
  125.           else
  126.                while(*(++*argv)!='\0')
  127.                     switch(**argv)
  128.                     {
  129.                          case 'w':
  130.                               if(*++argv!=NULL)
  131.                               {
  132.                                    wrap_index = atoi(*argv);
  133.                                    *argv+=(strlen(*argv)-1);
  134.                                    break;
  135.                               }
  136.                          default:
  137.                               usage();
  138.                               return 1;
  139.                     }
  140.      }
  141.  
  142.      indent=TRUE;
  143.      memset(t,'\0',ML-1);
  144.      while((c=getchar())!=EOF)
  145.      {
  146.           if(c=='%')             /* TeX comments */
  147.                while((c=getchar())!=EOF && c!='\n')
  148.                     ;
  149.           if(cr && c=='\n')
  150.           {
  151.                if(t_index)
  152.                {
  153.                     t[t_index]='\0';
  154.                     while(t[0]!='\0')
  155.                          force_out(t);
  156.                }
  157.                t_index=0;
  158.                indent=TRUE;   /* paragraph beginning */
  159.           }
  160.           if(control && isspace(c))
  161.                continue;
  162.           else
  163.                control=FALSE;
  164.           if(strchr("${}",c))      /* ignore these characters: ${} */
  165.                continue;
  166.           if(c=='\n')
  167.           {
  168.                cr++;
  169.                if(print_line)
  170.                {
  171.                     cr=0;
  172.                     t[t_index]='\0';
  173.                     kilspaces(t);
  174.                     if(center && wrap_index > strlen(t))
  175.                          for(i=0;i<(wrap_index-strlen(t))/2;i++)
  176.                               putchar(' ');
  177.  
  178.                     for(i=0;t[i]!='\0';i++)
  179.                          if(t[i]=='~')
  180.                               putchar(' ');
  181.                          else
  182.                               putchar(t[i]);
  183.                     putchar('\n');
  184.  
  185.                     t_index = 0;
  186.                     indent=FALSE;
  187.                     print_line = FALSE;
  188.                     center = FALSE;
  189.                     memset(t,'\0',ML);
  190.  
  191.                     continue;
  192.                }
  193.                else
  194.                     c=' ';
  195.           }
  196.           else
  197.                cr=0;
  198.  
  199.           if(c!='\\')
  200.                t[t_index++]=c;
  201.           else
  202.           {
  203.             control_code:
  204.                control=TRUE;
  205.                i=0;
  206.                while((c=getchar())!=EOF
  207.                     && !strchr("{}\\",c)
  208.                     && !isspace(c))
  209.                {
  210.                     s[i++]=c;
  211.                     if(!isalnum(c))
  212.                          break;
  213.                }
  214.                s[i]='\0';
  215.                cc=c;
  216.                if(streq(s,"-") || streq(s,"\""))
  217.                     ;         /* ignore */
  218.                else if(streq(s,"%"))
  219.                     t[t_index++]='%';
  220.                else if(streq(s,"bf"))
  221.                     ;
  222.                else if(streq(s,"bigskip"))
  223.                {
  224.                     t[t_index]='\0';
  225.                     while(t[0]!='\0')
  226.                          force_out(t);
  227.                     putchar('\n');
  228.                     t_index = 0;
  229.                }
  230.                else if(streq(s,"centerline"))
  231.                {
  232.                     center++;
  233.                     print_line++;
  234.                }
  235.                else if(streq(s,"chap"))
  236.                {
  237.                     sprintf(t+t_index," [%d] ",chapter++);
  238.                     if(chapter >= 100)
  239.                          t_index+= 2;
  240.                     else if(chapter >= 10)
  241.                          t_index += 1;
  242.                     t_index += strlen(" [1] ");
  243.                }
  244.                else if(streq(s,"dag"))
  245.                     t[t_index++]='%';
  246.                else if(streq(s,"end"))
  247.                     ;
  248.                else if(streq(s,"hbox"))
  249.                     ;
  250.                else if(streq(s,"input"))
  251.                     while((c=getchar())!='\n')
  252.                          ;
  253.                else if(streq(s,"langle"))
  254.                     t[t_index++]='<';
  255.                else if(streq(s,"line"))
  256.                     print_line++;
  257.                else if(streq(s,"linline"))
  258.                {
  259.                     print_line++;
  260.                     indent++;
  261.                }
  262.                else if(streq(s,"lline"))
  263.                     print_line++;
  264.                else if(streq(s,"narrower"))
  265.                     ;
  266.                else if(streq(s,"noindent"))
  267.                     indent=FALSE;
  268.                else if(streq(s,"poetrysize"))
  269.                     wrap_index=50;
  270.                else if(streq(s,"raggedbottom"))
  271.                     ;
  272.                else if(streq(s,"rangle"))
  273.                     t[t_index++]='>';
  274.                else if(streq(s,"sc"))
  275.                     ;
  276.                else if(streq(s,"sec"))
  277.                     ;
  278.                else if(streq(s,"vbox"))
  279.                     ;
  280.                else if(streq(s,"vfill"))
  281.                     ;
  282.                if((c=cc)=='\\')
  283.                     goto control_code;
  284.           }
  285.           if( !print_line && isspace(t[t_index-1]) && t_index >
  286.                                                   wrap_index+20 )
  287.           {
  288.                t[t_index]='\0';
  289.                force_out(t);
  290.                t_index = strlen(t);
  291.           }
  292.      }
  293.      return 0;
  294. }
  295.